package com.rattsoftware.server.serviceImpl;

import java.util.List;

import javax.servlet.ServletException;

import org.springframework.web.context.WebApplicationContext;

import com.rattsoftware.server.admin.model.User;
import com.rattsoftware.server.admin.model.exceptions.RattsSecurityException;
import com.rattsoftware.server.admin.model.exceptions.UserNotFoundException;
import com.rattsoftware.server.admin.model.paging.PagingLoadConfig;
import com.rattsoftware.server.admin.model.paging.PagingLoadResult;
import com.rattsoftware.server.rpc.RattsPersistentRemoteService;

public class UserServiceImpl extends RattsPersistentRemoteService implements com.rattsoftware.client.service.UserService {

	/** Generated serialization ID.*/
	private static final long serialVersionUID = 2600958435557581185L;
	private com.rattsoftware.server.service.UserService userService;
	private com.rattsoftware.server.service.AuthenticationService authenticationService;

	public UserServiceImpl() {
	}

	@Override
	public void init() throws ServletException {
		super.init();
		WebApplicationContext ctx = getApplicationContext();
		userService = (com.rattsoftware.server.service.UserService) ctx.getBean("userService");
		authenticationService = (com.rattsoftware.server.service.AuthenticationService) ctx.getBean("authenticationService");
	}

	@Override
	public void deleteUser(User user) {
		userService.deleteUser(user);
	}

	@Override
	public User saveUser(User user) {
		return userService.saveUser(user);
	}

	@Override
	public void saveMyUser(User user) {
		userService.saveMyUser(user);
	}

	@Override
	public User getUser(String username) throws UserNotFoundException {
		return userService.findUserByUsername(username);
	}

	@Override
	public Boolean checkIfUserChangedPassword(User user) {
		return userService.checkIfUserChangedPassword(user);
	}

	@Override
	public void logout() {
		userService.logout();
	}

	@Override
	public User getLoggedInUser() {
		User myUser = userService.getLoggedInUser();
		return myUser;
	}

	@Override
	public User findUserByEmail(String email) throws UserNotFoundException {
		return userService.findUserByEmail(email);
	}

	@Override
	public void resetPassword(User user, int size) {
		userService.resetPassword(user, size);
	}

	@Override
	public boolean validatePassword(User user) {
		return authenticationService.isValidUserPassword(user.getUsername(), user.getPassword());
	}

	@Override
	public User authenticate(String username, String password) {
		return authenticationService.authenticate(username, password);
	}

	@Override
	public void saveUsers(List<User> users) throws RattsSecurityException {
		userService.saveUsers(users);
	}

	@Override
	public void deleteUsers(List<User> users) throws RattsSecurityException {
		userService.deleteUsers(users);
	}

	@Override
	public PagingLoadResult<User> getUsers(PagingLoadConfig pagingLoadConfig)
		throws RattsSecurityException {
		return userService.getUsers(pagingLoadConfig);
	}

	@Override
	public List<User> getUsers() throws RattsSecurityException {
		return userService.getUsers();
	}
}
